package com.coderknock.wrapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.coderknock.jedis.executor.JedisExecutor;
import com.coderknock.pojo.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
import java.util.Enumeration;

/**
 * <p></p>
 *
 * @author 三产
 * @version 1.0
 * @date 2017-08-26
 * @QQGroup 213732117
 * @website http://www.coderknock.com
 * @copyright Copyright 2017 拿客 coderknock.com  All rights reserved.
 * @since JDK 1.8
 */
public class HttpSessionWrapper implements HttpSession {
    protected final Logger logger = LogManager.getLogger(HttpSessionWrapper.class);
    private String sid = "";
    private HttpServletRequest request;
    private HttpServletResponse response;
    private final long creationTime = System.currentTimeMillis();
    private final long lastAccessedTime = System.currentTimeMillis();
    //过期时间单位秒
    private int expire_time = 60;

    public HttpSessionWrapper() {
    }

    public HttpSessionWrapper(String sid, HttpServletRequest request,
                              HttpServletResponse response) {
        this.sid = sid;
        this.request = request;
        this.response = response;
    }

    public Object getAttribute(String name) {
        logger.info(getClass() + "getAttribute(),name:" + name);
        try {
            Object obj = JedisExecutor.execute(jedis -> {
                String jsonStr = jedis.get(sid + ":" + name);
                if (jsonStr != null || StringUtils.isNotEmpty(jsonStr)) {
                    jedis.expire(sid + ":" + name, expire_time);// 重置过期时间
                }
                return jsonStr;
            });
            return obj;
        } catch (JSONException je) {
            logger.error(je);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }

    public void setAttribute(String name, Object value) {
        logger.info(getClass() + "setAttribute(),name:" + name);
        try {
            JedisExecutor.executeNR(jedis -> {
                if (value instanceof String) {
                    String value_ = (String) value;
                    jedis.set(sid + ":" + name, value_);//普通字符串对象
                } else {
                    jedis.set(sid + ":" + name, JSON.toJSONString(value));//序列化对象
                }

                jedis.expire(sid + ":" + name, expire_time);// 重置过期时间
            });
        } catch (Exception e) {
            logger.error(e);
        }

    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public void invalidate() {
        logger.info(getClass() + "invalidate()");
    }

    public void removeAttribute(String name) {
        logger.info(getClass() + "removeAttribute(),name:" + name);
        if (StringUtils.isNotEmpty(name)) {
            try {
                JedisExecutor.executeNR(jedis -> {
                    jedis.del(sid + ":" + name);
                });
            } catch (Exception e) {
                logger.error(e);
            }
        }

    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public Object getValue(String name) {
        return null;
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public Enumeration getAttributeNames() {
        return null;

    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public String[] getValueNames() {
        return null;
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public void putValue(String name, Object value) {
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public void removeValue(String name) {
    }

    public long getCreationTime() {
        return creationTime;
    }

    public String getId() {
        logger.info(getClass() + "getId():" + sid);
        return sid;
    }

    public long getLastAccessedTime() {
        return lastAccessedTime;
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public ServletContext getServletContext() {
        return null;
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public void setMaxInactiveInterval(int interval) {
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public int getMaxInactiveInterval() {
        return 0;
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public HttpSessionContext getSessionContext() {
        return null;
    }

    /**
     * 不可用
     *
     * @deprecated
     */
    public boolean isNew() {
        logger.info(getClass() + "isNew()");
        return false;
    }
}